package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import com.atguigu.gulimall.product.vo.AttrVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional  //因为要操作两张表 所以开启事务 同时成功或失败
    @Override
    public void saveAttr(AttrVO attrVo) {
        AttrEntity attrEntity =new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        baseMapper.insert(attrEntity);
        AttrAttrgroupRelationEntity relationEntity =new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationDao.insert(relationEntity);
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        if (catelogId != 0){
            queryWrapper.eq("catelog_id",catelogId);
        }
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(obj ->{
                obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        //还需要查 每条记录的categoryName 和 attrGroupName
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVO> attrRespVOS = records.stream().map(item -> {
            AttrRespVO attrRespVO = new AttrRespVO();
            BeanUtils.copyProperties(item, attrRespVO);
            Long catelogId1 = item.getCatelogId();
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId1);
            String categoryName =null;
            if(categoryEntity!= null){
                 categoryName = categoryEntity.getName();
            }
            QueryWrapper<AttrAttrgroupRelationEntity> relationQueryWrapper = new QueryWrapper<>();
            relationQueryWrapper.eq("attr_id", item.getAttrId());
            AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(relationQueryWrapper);
            String attrGroupName =null;
            if(relationEntity !=null){
                Long attrGroupId = relationEntity.getAttrGroupId();
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
                 attrGroupName = attrGroupEntity.getAttrGroupName();
            }

            attrRespVO.setAttrGroupName(attrGroupName);
            attrRespVO.setCategoryName(categoryName);
            return attrRespVO;    //注意return
        }).collect(Collectors.toList());

        pageUtils.setList(attrRespVOS);
        return pageUtils;
    }

}